﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.BinLocationDefinitions;
using WMS.BinLocationDefinitionService;
using WMS.BinLocationDefinitionService.Dto;
using WMS.ClientService.Dto;
using WMS.Common;
using WMS.TakenOffs;
using WMS.TakenoffService.Dto;

namespace WMS.BinLocationDefinitionServicess
{
    public class BinLocationDefinitionServices:ApplicationService,IBinLocationDefinitionService
    {
        private readonly IRepository<BinLocationDefinition,int> repository;

        public BinLocationDefinitionServices(IRepository<BinLocationDefinition,int> repository)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 获取库位定义数据
        /// </summary>
        /// <param name="index"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetBinlocationdata(int index = 1, int limit = 10)
        {
            var predicate = PredicateBuilder.New<BinLocationDefinition>(true);
            var clientlist = repository.GetListAsync(predicate).Result;
            PagedResult result = new PagedResult();
            result.CurrentPage = index;
            result.PageSize = limit;
            result.RowCount = clientlist.Count();
            result.Queryable = clientlist.OrderBy(x => x.Id).Skip((index - 1) * limit).AsQueryable();
            result.Queryable = result.Queryable.MapToList<SearchBinlocationdto>().AsQueryable();
            return result;
        }

        /// <summary>
        /// 删除库位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteBins(int id)
        {
            await repository.DeleteAsync(id);
            return 1;
        }

        /// <summary>
        /// 添加库位信息
        /// </summary>
        /// <param name="addBinlocationdto"></param>
        /// <returns></returns>
        public async Task<int> AddTBinlocas(AddBinlocationdto addBinlocationdto)
        {
            var binnmodel = addBinlocationdto.MapTo<BinLocationDefinition>();
            await repository.InsertAsync(binnmodel);
            return 1;
        }

        /// <summary>
        /// 通过id获取库位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SearchBinlocationdto> SelectByidBins(int id)
        {
            var entity = repository.GetAsync(id).Result;
            var bintdto = entity.MapTo<SearchBinlocationdto>();
            return bintdto;
        }

        /// <summary>
        /// 修改库位信息
        /// </summary>
        /// <param name="searchBinlocationdto"></param>
        /// <returns></returns>
        public async Task<int> UpdateClients(SearchBinlocationdto searchBinlocationdto)
        {
            var entity = searchBinlocationdto.MapTo<BinLocationDefinition>();
            await repository.UpdateAsync(entity);
            return 1;
        }
    }
}
